package com.dhcc.sds.custom.service.impl;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcDictionaryEntity;
import com.dhcc.cdc.busi.service.ICdcDictionaryService;
import com.dhcc.cdc.sys.entity.ConfigurationEntity;
import com.dhcc.core.config.properties.SysProperties;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.security.ShiroKit;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.system.entity.User;
import com.dhcc.sdc.busi.entity.SdcBusiQcMrlistEntity;
import com.dhcc.sds.base.dao.SdsQcentityMapper;
import com.dhcc.sds.base.entity.SdsQcentityEntity;
import com.dhcc.sds.base.service.ISdsCommonService;
import com.dhcc.sds.busi.entity.SdsQcincaseOperlogEntity;
import com.dhcc.sds.custom.dao.SdsCusMrlistMapper;
import com.dhcc.sds.custom.entity.SdsCusMrlistEntity;
import com.dhcc.sds.custom.entity.SdsCusMrlistOperlogEntity;
import com.dhcc.sds.custom.service.ISdsCusMrlistService;
import com.dhcc.sds.standard.dto.CdcEpisodeDto;
import com.dhcc.sds.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 自定义病种入组病例表数据服务层接口
 *
 * @author yanghangwei
 * @ClassName: SdsCusMrlistServiceImpl
 * @Description: TODO
 * @date 2024-06-21 16:55
 */
@Service("sdsCusMrlistService")
public class SdsCusMrlistServiceImpl extends BaseServiceImpl<SdsCusMrlistMapper, SdsCusMrlistEntity> implements ISdsCusMrlistService {

    @Autowired
    private ICdcDictionaryService cdcDictionaryService;
    @Autowired
    private ISdsCommonService sdsCommonService;
    @Autowired
    private SysProperties sysPro;

    @Override
    public List<SdsCusMrlistEntity> page(Page<SdsCusMrlistEntity> page, Map<String, Object> map) {
        return this.baseMapper.page(page,map);
    }

    @Override
    public List<SdsCusMrlistEntity> list(Map<String, Object> map) {
        return this.baseMapper.list(map);
    }

    @Override
    public void changeStatus(Map<String, Object> map) {
        String execUser;
        if (CommonUtil.isNotEmpty(map.get("execUser"))) {
            execUser = map.get("execUser").toString();
        } else {
            // 获取当前登录用户的部门
            User user = ShiroKit.getUser();
            execUser = user.getLoginName();
        }
        String operStartTime = "";
        if (map.get("operStartTime")!=null){
            operStartTime = map.get("operStartTime").toString();
        }
        Long mrListId = Long.valueOf(map.get("mrListId").toString());
        Date execDate = new Date();
        SdsCusMrlistEntity cusMrlistEntity = selectById(mrListId);
        String status = map.get("status").toString();
        cusMrlistEntity.setId(mrListId);
        cusMrlistEntity.setState(status);
        cusMrlistEntity.setOutDate(execDate);
        cusMrlistEntity.setOutTime(execDate);
        cusMrlistEntity.setId(mrListId);
        boolean b = updateById(cusMrlistEntity);
        SdsCusMrlistOperlogEntity cusMrlistOperlogEntity = new SdsCusMrlistOperlogEntity();
        cusMrlistOperlogEntity.setInCaseId(mrListId);
        cusMrlistOperlogEntity.setOperState(status);
        if (map.get("reason")!=null) {
            cusMrlistOperlogEntity.setOperReason(String.valueOf(map.get("reason")));
        }
        if (map.get("resume")!=null) {
            cusMrlistOperlogEntity.setOperResume(String.valueOf(map.get("resume")));
        }
        cusMrlistOperlogEntity.setOperUser(execUser);
        cusMrlistOperlogEntity.setOperDate(execDate);
        if (CommonUtil.isNotEmpty(operStartTime)){
            Date date = DateUtil.parseTime(operStartTime);
            cusMrlistOperlogEntity.setOperStartDate(date);
        }
        cusMrlistOperlogEntity.insert();


    }

    @Override
    public List<CdcEpisodeDto> pageEpisodeByDateAndFormStatus(Page<CdcEpisodeDto> page, Map<String, Object> map) {
        //就诊类型
        CdcDictionaryEntity dictionaryEntity = cdcDictionaryService.queryByTypeCodeAndCode("PatData", "bus_adm_type", "I");
        //就诊状态
        CdcDictionaryEntity dictionaryEntity2 = cdcDictionaryService.queryByTypeCodeAndCode("PatData", "bus_adm_status", "C");

        Long entityTypeId = sdsCommonService.getDictId("QcentityType","Custom");
        map.put("busAdmTypeId", dictionaryEntity.getId());
        map.put("busAdmStatusId", dictionaryEntity2.getId());
        map.put("entityTypeId",entityTypeId);

        if (CommonUtil.isEmpty(map.get("admLocId"))) {
            List<String> deptIds = sdsCommonService.getDeptIdsBySysUser();
            map.put("userLinkDepts", deptIds);
        }

        List<CdcEpisodeDto> episodeDtoList =  baseMapper.selectAllByDateAndFormStatus(page, map);

        return episodeDtoList;
    }

    @Override
    @Transactional
    public String createExcel(Map<String, Object> map) {
        List<CdcEpisodeDto> episodeDtoList =  baseMapper.selectAllByDateAndFormStatus(map);

        String filePath = sysPro.getSysHome()+"file_download/sds/customgroup/";
        File file = new File(filePath);
        if (!file.exists()) file.mkdirs();
        String fileName = "自定义病种病例导出数据_" + com.dhcc.core.framework.util.DateUtil.getDays() + ".xlsx";
        File outFile = new File(filePath + fileName);
        try {
            EasyExcel.write(new FileOutputStream(outFile), CdcEpisodeDto.class).sheet("入组病例列表").doWrite(episodeDtoList);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return fileName;
    }
}
